|
Download
FAQ History |
|
API
Search Feedback |
Using JAXR Clients in J2EE Applications
You can create J2EE applications that use JAXR clients to access registries. This section explains how to write, compile, package, deploy, and run a J2EE application that uses JAXR to publish an organization to a registry and then query the registry for that organization. The application in this section uses two components, an application client and a stateless session bean.
The section covers the following topics:
You will find the source files for this section in the directory
<INSTALL>/j2eetutorial14/examples/jaxr/clientsession. Path names in this section are relative to this directory.Coding the Application Client: MyAppClient.java
The application client class,
src/MyAppClient.java,obtains a handle to the PubQuery enterprise bean's remote home interface, using the JNDI API naming contextjava:comp/env. The program then creates an instance of the bean and calls the bean's two business methods,executePublishandexecuteQuery.Before you compile the application, edit the
PubQueryBeanExamples.propertiesfile the same way you edited theJAXRExamples.propertiesfile to run the simple examples.
- Leave the
queryManagerURLandlifeCycleManagerURLentries as they are if you are using the Java WSDP Registry Server on your local system. To use another registry, comment out the property that specifies the Registry Server and remove the comment from the other registry.- If you are using a public registry, change the values for the
http.proxyHostandhttps.proxyHostentries so that they specify the the system on your network through which you access the Internet.Coding the PubQuery Session Bean
The PubQuery bean is a stateless session bean with one
createmethod and two business methods. The bean uses remote interfaces rather than local interfaces because it is accessed from outside the EJB container.The remote home interface source file is
src/PubQueryHome.java.The remote interface,
src/PubQueryRemote.java, declares two business methods,executePublishandexecuteQuery.The bean class,src/PubQueryBean.java,implements theexecutePublishandexecuteQuerymethods and their helper methodsgetName,getDescription, andgetKey. These methods are very similar to the methods of the same name in the simple examplesJAXRQuery.javaandJAXRPublish.java. TheexecutePublishmethod uses information in the filePubQueryBeanExample.propertiesto create an organization named The Coffee Enterprise Bean Break. TheexecuteQuerymethod uses the string "Coff", specified in the application client code, to locate this organization.The bean class also implements the required methods
ejbCreate,setSessionContext,ejbRemove,ejbActivate, andejbPassivate.The
ejbCreatemethod of the bean class allocates resources--in this case, by looking up theConnectionFactoryand creating theConnection.The
ejbRemovemethod must deallocate the resources that were allocated by theejbCreatemethod. In this case, theejbRemovemethod closes theConnection.Compiling the Source Files
To compile the application source files, use the following command:
The
compiletarget places the properties file and the class files in thebuilddirectory.Starting the J2EE Application Server
To run this example, you need to start the J2EE Application Server.
Creating JAXR Resources
In order to use JAXR in a J2EE application that uses the J2EE 1.4 Application Server, you need to access the JAXR resource adapter (see Implementing a JAXR Client. The following commands (each must be all on one line) create the resources needed to access the resource adapter:
asadmin create-connector-connection-pool --userusername--passwordpassword--raname jaxr-ra --connectiondefinition javax.xml.registry.ConnectionFactory jaxr-pool asadmin create-connector-resource --userusername--passwordpassword--poolname jaxr-pool eis/JAXRThe
build.xmlfile for this example has targets namedcreate-poolandcreate-resourcethat automate this task. Thecreate-resourcetask depends oncreate-pool.After you start the server, perform the following steps:
If the output from this command says that an instance restart is required, stop and restart the server.
Creating and Packaging the Application
Creating and packaging this application involve several steps:
Starting the J2EE Deploytool and Creating the Application
Packaging the Application Client
- Choose File
New
Application Client JAR to start the Application Client Wizard, then click Next.
- In the JAR File Contents screen:
- Make sure that Create New AppClient Module in Application is selected and that the application is
ClientSessionApp.- In the AppClient Display Name field, type
MyAppClient.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
clientsession/builddirectory. SelectMyAppClient.classfrom the Available Files tree area and click Add, then OK.- In the General screen, select
MyAppClientin the Main Class combo box.- In the EJB References screen, click Add. In the dialog box:
- Type
ejb/remote/PubQueryin the Coded Name field.- Choose Session from the EJB Type menu.
- Type
PubQueryHomein the Home Interface field.- Type
PubQueryRemotein the Local/Remote Interface field.- Leave the Enterprise Bean Name field empty.
When you return to the EJB References screen, choose the JNDI Name radio button and type
PubQuery. The session bean uses remote interfaces, so the client accesses the bean through the JNDI name rather than the bean name.- Click Finish.
Packaging the Session Bean
- Choose File
New
Enterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
- In the EJB JAR screen:
- Select Create New JAR Module in Application and make sure that the application is
ClientSessionApp.- In the JAR Display Name field, type
PubQueryJAR.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
clientsession/builddirectory. SelectPubQueryBean.class,PubQueryHome.class,PubQueryRemote.class, andPubQueryBeanExample.propertiesfrom the Available Files tree area and click Add, then OK.- In the General screen:
- Choose the Session radio button, then the Stateless radio button.
- From the Enterprise Bean Class menu, choose
PubQueryBean.- In the Enterprise Bean Display Name field, type
PubQuery.- In the Remote Interfaces area, choose
PubQueryHomefrom the Remote Home Interface menu andPubQueryRemotefrom the Remote Interface menu.- In the Configuration Options screen, check the Resource References and Transaction Management boxes.
- In the Transaction Management screen, choose Container-Managed.
- In the Resource References screen:
- Click Add.
- In the Coded Name field, type
eis/JAXR.- From the Type menu, choose
javax.xml.registry.ConnectionFactory.- In the Deployment Settings area, type
eis/JAXRin the JNDI name field andj2eein both the User Name and Password fields. (These are the user name and password specified in the file<J2EE_HOME>/domains/domain1/server/config/sun-acc.xml, which the application client container uses.)Checking the JNDI Names
Verify that the JNDI names for the application components are correct. They should appear as shown in Table 10-3 and Table 10-4.
Table 10-4 References Pane Ref. Type Referenced By Reference Name JNDI Name EJB Ref MyAppClient ejb/remote/PubQuery PubQuery Resource PubQuery eis/JAXR eis/JAXR
Deploying the Application
- Choose File
Save to save the application.
- Choose Tools
Deploy.
- In the dialog box, type your administrative user name and password (if they are not already filled in) and click OK.
- In the dialog box that asks if you wish to save the application, click Yes.
- In the Distribute Module dialog box, click Close when the process completes.
Saving the Client JAR and Running the Application
- If you are using the Java WSDP Registry Server, start it by starting Tomcat.
Windows systems: Choose Start
Programs
Java(TM) Web Services Developer Pack
Start Tomcat.
UNIX systems:
<JWSDP_HOME>/bin/startup.shStarting Tomcat takes some time.
- In the deploytool, select the server node.
- Select
ClientSessionAppfrom the list of deployed objects, then click Client Jar.- Choose Browse to navigate to the directory from which you will run the client; we suggest the
examples/jaxr/clientsessiondirectory. When you reach the directory, click Select, then click OK. Click OK in the information dialog. You will find a file namedClientSessionAppClient.jarin the specified directory.- To run the client, use the following command:
appclient -client ClientSessionAppClient.jarThe program output in the terminal window looks like this:
In the server log, you will find the output from the
executePublishandexecuteQuerymethods.Undeploying and Removing the Application
To undeploy and remove the application, perform the following steps:
If you wish, you can manually delete the EAR and client JAR files.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.